package dao

import (
	"activity_srv/activity"
	"activity_srv/model"
	"encoding/json"
	"errors"
	"fmt"
	"go.uber.org/zap"
	model2 "homepage_srv/model"
	"log"
	"math/rand"
	MSMQ "smart-health-platforms/inits"
	"smart-health-platforms/pkg"
	"smart-health-platforms/untils"
	"time"
	modele3 "user_srv/model"
)

// 秒杀前
func CreateSeckillGood(in *activity.CreateSeckillGoodRequest) (*model.SeckillGood, error) { //创建秒杀商品
	var goodSku model2.GoodSku
	err := goodSku.GetByGoodId(int(in.GoodskuId))
	if err != nil {
		pkg.Error("获取商品失败", zap.Error(err))
		return nil, errors.New("获取商品失败")
	}
	if goodSku.Id == 0 {
		pkg.Error("商品不存在", zap.Error(err))
		return nil, errors.New("商品不存在")
	}
	if goodSku.Stock < uint32(in.StockCount) {
		pkg.Error("商品库存不足", zap.Error(err))
		return nil, errors.New("商品库存不足")
	}
	startTime, err := time.Parse(time.DateTime, in.StartTime)
	if err != nil {
		return nil, fmt.Errorf("时间格式转换失败")
	}
	endTime, err := time.Parse(time.DateTime, in.EndTime)
	if err != nil {
		return nil, fmt.Errorf("时间格式转换失败")
	}

	seckillGood := &model.SeckillGood{
		GoodskuId:  uint(in.GoodskuId),
		StockCount: uint(in.StockCount),
		StartTime:  startTime,
		EndTime:    endTime,
		Price:      float64(in.Price),
	}
	goodSku.Stock -= uint32(in.StockCount)
	err = goodSku.UpdateSeckillStock()
	if err != nil {
		pkg.Error("更新商品库存失败", zap.Error(err))
		return nil, errors.New("更新商品库存失败")
	}

	err = seckillGood.CreateSeckillGood() // 创建秒杀商品

	if err != nil {
		pkg.Error("创建秒杀商品失败", zap.Error(err))
		return nil, errors.New("创建秒杀商品失败")
	}
	untils.SyncSeckillStock(int(seckillGood.Id), int(seckillGood.StockCount)) //同步秒杀库存
	return seckillGood, err

}

// 秒杀中
func CreateSeckillOrder(in *activity.CreateSeckillOrderRequest) (*model.SeckillOrder, error) {
	var user modele3.Users
	err := user.GetUserInfoById(int(in.UserId))
	if err != nil {
		pkg.Error("获取用户失败", zap.Error(err))
		return nil, errors.New("获取用户失败")
	}
	if user.Id == 0 {
		pkg.Error("用户不存在", zap.Error(err))
		return nil, errors.New("用户不存在")
	}
	var seckillGood model.SeckillGood
	err = seckillGood.GetSeckillGood(int(in.SeckillGoodId))
	if err != nil {
		pkg.Error("获取秒杀商品失败", zap.Error(err))
		return nil, errors.New("获取秒杀商品失败")
	}
	if seckillGood.Id == 0 {
		pkg.Error("秒杀商品不存在", zap.Error(err))
		return nil, errors.New("秒杀商品不存在")
	}
	t := time.Now()
	if t.Before(seckillGood.StartTime) {
		pkg.Error("秒杀未开始", zap.Error(err))
		return nil, errors.New("秒杀未开始")
	}
	if t.After(seckillGood.EndTime) {
		pkg.Error("秒杀已结束", zap.Error(err))
		return nil, errors.New("秒杀已结束")
	}
	if seckillGood.StockCount < 1 {
		pkg.Error("秒杀商品已售罄", zap.Error(err))
		return nil, errors.New("秒杀商品已售罄")
	}
	// 扣减秒杀库存
	if untils.UpdateSeckillStock(int(seckillGood.Id)) == false {
		pkg.Error("扣减秒杀库存失败", zap.Error(err))
		return nil, errors.New("扣减秒杀库存失败")
	}

	seckillOrder := &model.SeckillOrder{
		OrderSn:       "MS" + time.Now().Format("20060102150405") + fmt.Sprintf("%06d", rand.Intn(1_000_000)),
		UserId:        uint(in.UserId),
		SeckillGoodId: uint(in.SeckillGoodId),
		SeckillPrice:  seckillGood.Price,
		Quantity:      1,
	}
	marshal, _ := json.Marshal(seckillOrder) //序列化
	rabbitmq := MSMQ.NewRabbitMQSimple("" +
		"2301a")
	_, _ = rabbitmq.PublishSimple(marshal) //发送消息

	log.Println("发送成功！")
	return seckillOrder, err
}
